package com.hymjweb.demo.hymj.basic_data_manage.xt_dic.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_dic.bean.XtDicCachePojo;
import com.hymjweb.demo.hymj.basic_data_manage.xt_dic.bean.XtDicPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface XtDicMapper {



    class XtDicMapperProvider{

        public String findAll(Map map) {

            String sql= new SQL() {{
                SELECT("xd.id, xd.name, xd.full_name,xd.pid, xd.dic_key, xd.status,standby1, xd.standby1_desc, xd.standby2, xd.standby2_desc, xd.standby3, xd.standby3_desc");
                FROM("xt_dic xd");
            }}.toString();

            if (map != null) {
                String id = DataTypeUtil.toNotNullString(map.get("id"));
                String name = DataTypeUtil.toNotNullString(map.get("name"));
                String pid = DataTypeUtil.toNotNullString(map.get("pid"));
                String dicKey = DataTypeUtil.toNotNullString(map.get("dicKey"));
                boolean isLeft = (boolean) map.get("isLeft");

                StringBuffer cnd = new StringBuffer(" where 1 = 1 ");
                if(isLeft){
                    cnd.append(" and xd.pid = " + DataTypeUtil.toInt(pid) + " and xd.id != 0");
                }else{
                    cnd.append(" and xd.pid = " + DataTypeUtil.toInt(pid));
                }
                StringBuffer orCnd = new StringBuffer("(");
                if(id != null && !"".equals(id)){
                    if(DataTypeUtil.isNumber(id)){
                        orCnd.append(" xd.id = " + DataTypeUtil.toInt(id));
                    }
                }
                if(orCnd.length() == 1){
                    if(name != null && !"".equals(name)){
                        orCnd.append(" xd.name like " + DataTypeUtil.quoteAllLikeStr(name));
                    }
                }else{
                    if(name != null && !"".equals(name)){
                        orCnd.append(" or xd.name like " + DataTypeUtil.quoteAllLikeStr(name));
                    }
                }
                if(orCnd.length() == 1){
                    if(dicKey != null && !"".equals(dicKey)){
                        orCnd.append(" xd.dic_key like " + DataTypeUtil.quoteAllLikeStr(dicKey));
                    }
                }else{
                    if(dicKey != null && !"".equals(dicKey)){
                        orCnd.append(" or xd.dic_key like " + DataTypeUtil.quoteAllLikeStr(dicKey));
                    }
                }
                orCnd.append(")");
                if(orCnd.length() > 2){
                    sql += cnd.toString() + " and " + orCnd.toString();
                }else{
                    sql +=  cnd.toString();
                }
            }
            return  sql;
        }

        public String findCacheAll() {
            return new SQL() {{
                SELECT("xd.id, xd.name, xd.full_name, xd.pid, xd.dic_key");
                FROM("xt_dic xd");
            }}.toString();
        }


        public String findObjectById(long id) {
            return new SQL() {{
                SELECT("xd.id, xd.pid, xd.name, xd.full_name, xd.dic_key, xd.status, xd.standby1, xd.standby1_desc, xd.standby2, xd.standby2_desc, xd.standby3, xd.standby3_desc, xd.create_time, xd.create_user_id, xd.update_time, xd.update_user_id");
                FROM("xt_dic xd");
                WHERE("xd.id = " + id);
            }}.toString();
        }

        public String findObjectByDicKey(String dicKey,int exceptId) {
            String sql = new SQL() {{
                SELECT("xd.id, xd.pid, xd.name, xd.dic_key");
                FROM("xt_dic xd");
                WHERE("xd.dic_key = " + DataTypeUtil.quoteStr(dicKey));
            }}.toString();
            if(exceptId != 0){
                sql += " and xd.id != " + exceptId;
            }
            return sql;
        }

        public String insert(XtDicPojo obj) {
            String sql= new SQL() {{
                INSERT_INTO("xt_dic");
                VALUES("id","#{id}");
                VALUES("pid","#{pid}");
                VALUES("name","#{name}");
                VALUES("full_name","#{fullName}");
                VALUES("dic_key","#{dicKey}");
                VALUES("status","#{status}");
                VALUES("standby1","#{standby1}");
                VALUES("standby1_desc","#{standby1Desc}");
                VALUES("standby2","#{standby2}");
                VALUES("standby2_desc","#{standby2Desc}");
                VALUES("standby3","#{standby3}");
                VALUES("standby3_desc","#{standby3Desc}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

        public String update(XtDicPojo obj) {
            String sql= new SQL() {{
                UPDATE("xt_dic xd");
                SET("xd.pid = #{pid}");
                SET("xd.name = #{name}");
                SET("xd.full_name = #{fullName}");
                SET("xd.dic_key = #{dicKey}");
                SET("xd.status = #{status}");
                SET("xd.standby1 = #{standby1}");
                SET("xd.standby1_desc = #{standby1Desc}");
                SET("xd.standby2 = #{standby2}");
                SET("xd.standby2_desc = #{standby2Desc}");
                SET("xd.standby3 = #{standby3}");
                SET("xd.standby3_desc = #{standby3Desc}");
                SET("xd.update_time = #{updateTime}");
                SET("xd.update_user_id = #{updateUserId}");
                WHERE("xd.id=#{id}");
            }}.toString();
            return sql;
        }

        public String delete(String id) {
            String sql = new SQL() {{
                DELETE_FROM("xt_dic");
                WHERE("id = " + id );
            }}.toString();
            return sql;
        }

        public String queryChildNodeById(String id) {
            return new SQL() {{
                SELECT("xd.id, xd.name");
                FROM("xt_dic xd " );
                WHERE("xd.pid = " + DataTypeUtil.quoteStr(id));
            }}.toString();
        }

        public String queryXtDicPojoByDicKey(String xtDickKey){
            return new SQL() {{
                SELECT("xd.*");
                FROM("xt_dic xd " );
                WHERE("xd.dic_key = #{xtDickKey}");
            }}.toString();
        }

    }

    @SelectProvider(type = XtDicMapperProvider.class, method = "findCacheAll")
    @Results(id="xtDicCacheMapId",value = {@Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "full_name", property = "fullName"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "dic_key", property = "dicKey")
    })
    List<XtDicCachePojo> findCacheAll();

    @Results(id = "xtDicMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "full_name", property = "fullName"),
            @Result(column = "pid", property = "pid"),
            @Result(column = "dic_key", property = "dicKey"),
            @Result(column = "status", property = "status"),
            @Result(column = "standby1", property = "standby1"),
            @Result(column = "standby1_desc", property = "standby1Desc"),
            @Result(column = "standby2", property = "standby2"),
            @Result(column = "standby2_desc", property = "standby2Desc"),
            @Result(column = "standby3", property = "standby3"),
            @Result(column = "standby3_desc", property = "standby3Desc"),
    })
    @SelectProvider(type = XtDicMapperProvider.class, method = "findAll")
    List<XtDicPojo> findAll(Map map);


    @ResultMap(value = "xtDicMapId")
    @SelectProvider(type = XtDicMapperProvider.class, method = "queryXtDicPojoByDicKey")
    List<XtDicPojo> queryXtDicPojoByDicKey(String xtDickKey);


    @ResultMap(value = "xtDicMapId")
    @SelectProvider(type = XtDicMapperProvider.class, method = "findObjectById")
    XtDicPojo findObjectById(@Param("id") int id);

    @Results(id = "childNodeMapId",value = {
            @Result(column = "id", property = "id"),
            @Result(column = "name", property = "name"),
    })
    @SelectProvider(type = XtDicMapperProvider.class, method = "queryChildNodeById")
    List<XtDicPojo> queryChildNodeById(String id);

    @InsertProvider(type = XtDicMapperProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(XtDicPojo obj);

    @UpdateProvider(type = XtDicMapperProvider.class, method = "update")
    int update(XtDicPojo obj);

    @DeleteProvider(type = XtDicMapperProvider.class, method = "delete")
    int delete(String id);
}
